//
//  FR_Attendance_ViewController.m
//  FaceRecognition
//
//  Created by zz on 17/7/17.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_Attendance_ViewController.h"
#import "FR_Attendance_TableViewCell.h"
#import "FR_Late_Fitter_ViewController.h"
#import "FR_Attendance_Model.h"

@interface FR_Attendance_ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *classesDataArr;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UILabel *emptyLabel;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, assign) BOOL isSchollManager;

@property (nonatomic, copy) NSString *classid; //班级id
@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSString *stuendId; //学生id 暂时未使用  使用学生姓名
@property (nonatomic, copy) NSString *stuName; //学生姓名

@property (nonatomic, copy) NSString *startTimeStr; //开始时间

@property (nonatomic, copy) NSString *endTimeStr; //结束时间

@end

@implementation FR_Attendance_ViewController

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight-SafeAreaBottomHeight ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self requestList];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self requestMoreList];
        }];
        
        _tableView.tableHeaderView = self.tableHeaderView;
        
        _tableView.rowHeight = 44;
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, VIEW_WIDTH, 15)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

- (NSMutableArray *)classesDataArr {
    
    if (!_classesDataArr) {
        
        _classesDataArr = [NSMutableArray array];
    }
    
    return _classesDataArr;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (UIView *)tableHeaderView {
    
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 56)];
        _tableHeaderView.backgroundColor = [UIColor whiteColor];
        
        CGFloat w = (SCREENWIDTH - 30) / 4;
        
        CGFloat rate = SCREENWIDTH / 375;
        for (int i = 0; i < 4; i ++) {
            
            CGFloat x = 15 + w * i;
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, w, 44)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:16];
            label.backgroundColor = [UIColor whiteColor];
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            [_tableHeaderView addSubview:label];
            
            if ( i == 0) {
                label.text = @"班级";
                
                label.frame = CGRectMake(15 * rate, 0, 99 * rate, 44);
                
            }else if (i == 1){
                label.text = @"姓名";
                
                label.frame = CGRectMake((15 + 99) * rate, 0, 82 * rate, 44);
                
            }else if (i == 2){
                label.text = @"日期";
                
                label.frame = CGRectMake((15 + 99 + 82) * rate, 0, 82 * rate, 44);
            }else {
                label.text = @"状态";
                
                label.frame = CGRectMake((15 + 99 + 82 + 82) * rate, 0, 82 * rate, 44);
            }
            
        }
        
        UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, VIEW_WIDTH, 12)];
        grayView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        [_tableHeaderView addSubview:grayView];
        
    }
    
    return _tableHeaderView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _date = @"";
    _classid = @"0";
    _stuName = @"";
    _startTimeStr = @"";
    _endTimeStr = @"";
    
    //判断是否是管理员
    self.isSchollManager = [SYAccountTool ShareAccount].account.schoolManage;
    

    
    self.navBar.nav_titleLabel.text = @"考勤";
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];

   self.navBar.rightButton.hidden = NO;
   [self.navBar.rightButton setTitle:@"筛选 " forState:UIControlStateNormal];
   [self.navBar.rightButton setTitleColor:[UIColor colorWithHexString:@"#1098fd"] forState:UIControlStateNormal];
   self.navBar.rightButton.titleLabel.font = [UIFont systemFontOfSize:17];
   [self.navBar.rightButton addTarget:self action:@selector(filterAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self requestList];
    
//    if (_isSchollManager) { //管理员直接请求列表
//        
//        [self requestList];
//        
//    }else {
//    
//        [self getManagerClasses];
//    }
    

}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
}

- (void)back {
    [super back];
    [SYAccountTool ShareAccount].attendance_dict = nil;
}

#pragma mark -Request
- (void)getManagerClasses{
    
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.schoolManage > 0?@"1":@"0" forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        [self.classesDataArr removeAllObjects];
        
        [self.classesDataArr addObjectsFromArray:array];
        
        [self requestList];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
    
}

- (void)requestList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"intervieweeid"];
    [dict setObject:@"1" forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    NSString *scid = [SYAccountTool ShareAccount].account.scid;
    if (scid.length == 0) {
        scid = @"0";
    }

    [dict setObject:scid forKey:@"scid"];
    [dict setObject:USERID forKey:@"teacherid"];
    
    
    
    [dict setObject:_date forKey:@"date"];
    [dict setObject:@"-1" forKey:@"arrival"];
    
    NSString *apiStr;
    if (_isSchollManager) {
        
        apiStr = @"api/getScAttendanceList";
        
    }else {
        apiStr = @"api/getClassAttendanceList";
        
    }
    
    [dict setObject:_classid forKey:@"classid"]; //classid 默认传0
    
//    if (_stuName.length > 0) { //学生姓名
    
        [dict setObject:_stuName forKey:@"studentname"];
//    }
    
   // if (_startTimeStr.length > 0) { //开始时间
        
        [dict setObject:_startTimeStr forKey:@"starttime"];
//    }
    
//    if (_endTimeStr.length > 0) {
    
        [dict setObject:_endTimeStr forKey:@"endtime"];
//    }
    
    

    
    [Utils Java_get:apiStr parameters:dict success:^(NSInteger recode, id jsonObject) {
        _pageNo = 1;
        [self.dataArr removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        self.emptyLabel.hidden = arr.count != 0;
        
        self.dataArr = [FR_Attendance_Model mj_objectArrayWithKeyValuesArray:arr];
        
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        
    }];
}

- (void)requestMoreList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"intervieweeid"];
    [dict setObject:@(_pageNo) forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    NSString *scid = [SYAccountTool ShareAccount].account.scid;
    
    if (scid.length == 0) {
        scid = @"0";
    }
    
    [dict setObject:scid forKey:@"scid"];
    [dict setObject:_date forKey:@"date"];
    [dict setObject:@"-1" forKey:@"arrival"];
    [dict setObject:USERID forKey:@"teacherid"];
    
    NSString *apiStr;
    if (_isSchollManager) { //管理员
        
        apiStr = @"api/getScAttendanceList";
        
//        if (_classid.length == 0) { //没有_classid就获取所有班级数据
//            
//            [dict setObject:@"0" forKey:@"classid"]; //传0查所有
//        }else {
//            [dict setObject:_classid forKey:@"classid"];
//        }
        
    }else {
        apiStr = @"api/getClassAttendanceList";
        
    }

    [dict setObject:_classid forKey:@"classid"];
    
//    if (_stuName.length > 0) { //学生姓名
    
        [dict setObject:_stuName forKey:@"studentname"];
//    }
    
//    if (_startTimeStr.length > 0) { //开始时间

        [dict setObject:_startTimeStr forKey:@"starttime"];
//    }
    
//    if (_endTimeStr.length > 0) {
    
        [dict setObject:_endTimeStr forKey:@"endtime"];
//    }
    
    [Utils Java_get:apiStr parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        NSArray *arrM = [FR_Attendance_Model mj_objectArrayWithKeyValuesArray:arr];
        [self.dataArr addObjectsFromArray:arrM];
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self.tableView.mj_footer endRefreshing];
        [self showHint:error.localizedDescription];
    }];
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellid = @"cellid";
    
    FR_Attendance_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    if (!cell) {
        
        cell = [[FR_Attendance_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    
    cell.model = [self.dataArr objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark -Action
- (void)filterAction {
    
    FR_Late_Fitter_ViewController *vc  = [[FR_Late_Fitter_ViewController alloc] init];
    vc.approvel = -1;
    vc.navTitle = @"考勤";
    vc.managerType = _isSchollManager ? 1 : 0;
    
    vc.confirmBlock = ^(NSMutableDictionary *dict) {
    
        //开始时间
        _startTimeStr = [dict objectForKey:@"_selDateStartStr"];
        
        //结束时间
        _endTimeStr = [dict objectForKey:@"_selDateEndStr"];
        
        _classid = [dict objectForKey:@"_selClassIdStr"];
        
        _stuName = [dict objectForKey:@"_selStuName"];
        
        
        if (!_startTimeStr) {
            _startTimeStr = @"";
        }
        
        if (!_endTimeStr) {
            
            _endTimeStr = @"";
        }
        
        if (!_classid || _classid.length == 0) {
            _classid = @"0";
        }
        
        [self requestList];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}


@end
